﻿using AirService.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AirService
{
    public partial class Service1 : ServiceBase
    {
        
        private UdpClient receiveUpdClient;
        private UdpClient receiveUpdClient2;
        private List<string> localip = new List<string> { "192.168.2.11", "1001","20002" };
        private List<string> sendIp = new List<string> { "192.168.2.11", "8080" };

        public Service1()
        {
            InitializeComponent();
        }

        //string filePath = @"D:\DevelopProgram\Logs\MyNewServiceLog.txt";

        protected override void OnStart(string[] args)
        {

            LogHelper.WriteLog($"{DateTime.Now},服务启动!开始接收数据...");
            
            udpConfigParame();
            ReceiveMessage();
        }

        protected override void OnStop()
        {
            LogHelper.WriteLog($"{DateTime.Now},服务停止！");
        }

        // 系统即将关闭执行方法
        protected override void OnShutdown()
        {
            base.OnShutdown();
        }

        //定时器方法
        protected void time1_Tick(object sender,EventArgs e) { 
            // 定时器任务
        }

        private void ReceiveMessage()
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            while (true)
            {
                try
                {
                    
                    byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);

                    //string message = Encoding.UTF8.GetString(receiveBytes);
                    string message = Encoding.ASCII.GetString(receiveBytes) + remoteIpEndPoint.Address + "_" + remoteIpEndPoint.Port;

                    string downMessage = "@5b,1,10002,{0},#0 0x7F 0xC9";
                    downMessage = string.Format(downMessage, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    byte[] sendBytes = System.Text.Encoding.Default.GetBytes(downMessage);

                    IPAddress remoteIp = IPAddress.Parse(remoteIpEndPoint.Address.ToString());
                    IPEndPoint remoteIpEndPointSend = new IPEndPoint(remoteIp, int.Parse(remoteIpEndPoint.Port.ToString()));      //端口发送和接收一样后，重复点击回报错。

                    // 回发消息
                    receiveUpdClient.Send(sendBytes, sendBytes.Length, remoteIpEndPointSend);
                    // 显示消息内容
                    LogHelper.WriteLog(string.Format("logRecord：{0}[{1}],{2},Record:{3}！", remoteIpEndPoint.Address.ToString(), remoteIpEndPoint.Port.ToString(), DateTime.Now, message));
                }
                catch (Exception)
                {
                    break;
                }
            }
        }
        private void udpConfigParame()
        {

            // 创建接收套接字
            IPAddress localIp = IPAddress.Parse(localip[0]);
            IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(localip[1]));        //端口发送和接收一样后，重复点击回报错。
            //绑定ip和port
            receiveUpdClient.Connect(localIpEndPoint);
            receiveUpdClient = new UdpClient();
/*            Thread receiveThread = new Thread(ReceiveMessage);
            receiveThread.Start();*/

            // 创建接收套接字
            IPAddress localReceiveIp = IPAddress.Parse(localip[0]);
            IPEndPoint localIpEndPointReceive = new IPEndPoint(localReceiveIp, int.Parse(localip[2]));        //端口发送和接收一样后，重复点击回报错。
            //绑定ip和port                                                                                         //绑定ip和port                                                                                              //绑定ip和port
            receiveUpdClient2.Connect(localIpEndPointReceive);
            receiveUpdClient2 = new UdpClient();
            /*Thread receiveThread2 = new Thread(ReceiveMessage);
            receiveThread2.Start();*/

        }



    }
}
